//
//  WeiboTabBarController.swift
//  SinaWeibo
//
//  Created by 风不会停息 on 2016/11/12.
//  Copyright © 2016年 风不会停息. All rights reserved.
//

import UIKit

class WeiboTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
   
        let tabbar = WeiboTabbar()
        self.setValue(tabbar, forKey: "tabBar")
        tabbar.closure =  { [weak self] in
            //在实际开发中 按钮的点击事件处理的地方 需要留一个查找的标记
            
//            print(self!)
            let composeView = WBComposeView()
            
            
            composeView.show(fromVC: self)
//            UIApplication.shared.keyWindow?.addSubview(composeView)
        }
        addChildViewControllers()
        
        
        
    }
    
    private func addChildViewControllers(){
        
        addChildViewController(vc: WeiboHomeViewController(), title: "首页", imageName: "tabbar_home",index:0)
        addChildViewController(vc: WeiboMessageViewController(), title: "消息", imageName: "tabbar_message_center",index:1)
        addChildViewController(vc: WeiboDiscoverViewController(), title: "发现", imageName: "tabbar_discover",index:2)
        addChildViewController(vc: WeiboProfileViewController(), title: "我", imageName: "tabbar_profile",index:3)
    }
    
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String,index: Int) {
        
     
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        vc.tabBarItem.tag = index
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置选中状态下的文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //设置字体的大小为10
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置tabbarItem图片和文字之间的距离
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        let nav = WeiboBaseNavController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print(item.tag)
        //1.应该获取到被点击的UITabBarButton
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //可以获取到被点击tabbarButton
                    //遍历子视图
                    for v in subView.subviews {
                        if v.isKind(of: NSClassFromString("UIImageView")!) {
//                            print("终于找到你,还好没放弃\(v)")
                            //线缩小
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
//                                print("👌")
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }

   

}
